| Wildform is the creator of the Flix SWF video encoder and the SWfX SWF text effects generator. This tutorial was written by FR Elkins. last modified December 13, 2000 ©2000 Wildform, Inc. |
| <- back to tutorials |
| Press-and-Hold Scrolling For an External Rich-Text File Using Buttons and Keys in Flash 5 |
| Table of Contents |
| Introduction |
This tutorial is a practical hands-on experience with Flash 5's new syntax in a useful context. It's geared to the new Flash 5 users, and the designer who may find Flash 5 daunting. More experienced users can look at the technique but might want to skim over the bright and cheery explanations. Although scrolling is a common and relatively simple effect, it's still one of the most asked about on the many Flash listservs. Also, the change of syntax from Flash 4 to Flash 5 can be scary to the designer. Press-and-hold scrolling in the Flash 5 context is a small but useful skill, because it's an easy way to see how the new "dot syntax" works on a practical level. Further, when combined with the new ability of Flash 5 to load an external rich text file, it gives our users (remember them?) the kinds of features they're used to. The first thing you need to understand is how Flash handles the two important properties SCROLL and MAXSCROLL. If you don't know offhand, don't worry we'll talk about them a little here. But you might want to skim over them in the Flash 5 manual (Actionscripting Guide, pp. 309 and 356); it will save you some time. Also, review the manual section on Flash 5's new rich-text text box features (Using Flash, p. 219). Again, we'll be talking about them here, but it will be helpful if you remember what HTML tags Flash supports in text boxes and which ones it doesn't. Basically, you get <A>, <B>, <I>, <U>, <P>, and <FONT>, with COLOR and SIZE. You don't get tables, lists, <DIV> or <SPAN>. Finally, we'll also talk about the Key object, the isDown method, and key constants (Actionscripting Guide, pp. 279-288). Finally, the instructions use the Windows right-click feature; of course on the Mac that becomes a Control+click. |
| Table of Contents |
| Drawing and Naming the Text Box |
|
| Table of Contents |
| Creating the Rich-Text File |
NB: If you need to create line breaks beyond what <p> does for you try using the hex characters %0d. You can get some space resembling a tab using %09. You can also insert bullet characters in the text file by using the numerical reference appropriate for the font. Check out your favorite character mapping utility for the codes. However, even if you use Courier as the font, it is difficult to approximate tables or hanging lists this way. So experiment, but don't depend on it. ![]() |
| Table of Contents |
| Adding Buttons |
Since we're building on Bill Tagliaferro's press-and-hold scrolling technique, we're going to have the buttons set variables to do our scrolling for us. Let's call the variable down. For our purposes, the user can only move up or down in our movie, and can only do one at a time. We'll make buttons, put them in a movie clip, and then add variables and actions to make the buttons work. And for designers newer to scripting, these techniques teach us to think of everything we want to happen in our movie as states of true or false, existence or non-existence.
onMouseEvent. Again the dialog expands to offer more options. Select the Press check box. Notice the code Flash writes in the window.
set variable. Again the dialog expands to offer more options. In Variable, type down; in Value, type true. This sets the value of down to be true "on" and means that whenever a user is pressing the Up button, the movie scrolls up. Choose onMouseEvent again, check Release, ReleaseOutside and set variable one more time. In Variable, type down; in Value, type false. This ends scrolling when the user releases the mouse.
down and tell the movie what to do. Click the Down movie clip underlined above the timeline to return to the Up clip. Right-click on the Up clip and choose Actions. The Object Actions dialog box opens;click the arrow in the upper right-hand corner to choose Expert Mode. This lets us type directly in the window, which for this section of code is easier than choosing from the list. Type:
onClipEvent (enterFrame) {
if (down) {
if (_root.text.scroll<_root.text.maxscroll) {
_root.text.scroll = _root.text.scroll+1;
}
}
}
In Flash 5, single frame movie clips loop. The OnClipEvent (enter Frame) tells the clip to check whether down is true every time it plays. That is, it checks to see if the user is clicking the Down button. If so, Flash goes off to find the Text variable. Notice how we tell Flash where that variable is. Remember, Text inhabits the main timeline, or root, of the movie, while our buttons dwell in nested movie clips on their own timelines. Once Flash has found Text, it then checks to see if it's at the beginning of the last set of lines that fits into the text box (the maxscroll). Flash sets the maxscroll based on the length of the text file and the size of the text box. We can't change it. We could have flash tells us the number of the maxscroll, but for our purposes here we don't care about the exact figure. As long as we haven't reached it, we want to keep going down. So Flash checks to make sure the current line number is less than the maxscroll, and if so, it scrolls down one more line using Text's scroll property.
OnClipEvent code is a little different:
onClipEvent (enterFrame) {
if (down) {
if (_root.text.scroll<=_root.text.maxscroll) {
_root.text.scroll = _root.text.scroll-1;
}
}
}
You probably got the idea by now.
scroll property; that is, we want Flash to scroll to the first line of the text box when it's pressed. However, we won't use the same tactic with the down variable here. Why not? Because testing shows you can easily work yourself into a situation with this method where both Page Up and Page Down can be true since we are setting the scroll to a certain number. Also, there are circumstances where even after a user releases the Up or Down, the movie can still think they are true, which makes it hard to go Home! We'll work around this now. So experience shows we should clear down when going Home. (An interesting experiment if you have 30 minutes is to apply the previous techniques to Home and End to see how the problems arise. Look at the variables in the Output window when you test the movie. But you may not have the time.) Instead, we'll just put some code on the Home button itself inside the Home movie clip:
on (press) {
down = false;
_root.text.scroll = "1";
}
on (press) {
down = false;
_root.text.scroll = _root.text.maxscroll;
}
We don't know precisely what line number is the maxscroll, and again, it doesn't matter. Flash figures it out for us. |
| Table of Contents |
| Adding the Key Script |
Amazing how we're
almost done! All we have to do is add the script that enables users to use the
up arrow, down arrow, Page Up and Page Down Keys.
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP) == true) {
if (_root.about.scroll<=_root.about.maxscroll) {
_root.about.scroll = _root.about.scroll-1;
}
} else if (Key.isDown(Key.DOWN) == true) {
if (_root.about.scroll<_root.about.maxscroll) {
_root.about.scroll = _root.about.scroll+1;
}
} else if (Key.isDown(Key.PGUP) == true) {
_root.about.scroll = 1;
} else if (Key.isDown(Key.PGDN) == true) {
_root.about.scroll = _root.about.maxscroll;
}
endif;
}
Not only do
we deal with the familiar scroll and maxscroll,
we also introduce the Key object and its isDown
method. The isDown looks to see if it's true
that the user is pressing a certain key. What certain key? The Key.UP
and Key.DOWN are the Flash 5 ways of referencing the keys.
Key tells Flash what object a key to look for;
the .Down tells it which particular key. From your review
of the Flash manual, you remember that Key.UP and Key.DOWN
are constants; that is, they are always going to mean the up and down
arrow keys; we can't make them mean anything else. So as long as the user
holds the up arrow key, Key.isDown(Key.Up) will be true,
and so the movie scrolls.Great! Now it works! Of course, one of the interesting things about scripting is that there are many ways to approach and solve problems. Some are shorter, more elegant, and more efficient than others. Almost all scripts can be improved and tightened. So you may find your own way to perform this task, or a better way. If so, write it up, and help out the Flash community! Don't forget to have fun doing it, too. |
| Table of Contents |
| <- back to tutorials |
| ©2000 Wildform, Inc. |